package disjunction.extensions.controller
{
	import disjunction.View;
	import disjunction.Controller;
	import disjunction.extensions.model.IEntityHoldingModel;
	
	/**
	 * Extends the Controller to enable periodic updates of entity lists.
	 *
	 * @author Nick Wiggill
	 */
	
	public class EntityManagingController extends Controller
	{
		public function EntityManagingController(model:IEntityHoldingModel, view:View)
		{
			super(model, view);
		}
		
		public function update(timeDeltaMs:uint):void
		{
			updateEntityLists();
			super.update(timeDeltaMs);
		}
		
		public function updateEntityLists():void
		{
			var entity:Entity;
			
			//remove/retire old entities from last tick
			for each (entity in model.entitiesToRemove)
			model.entities.splice(model.entities.lastIndexOf(entity, 0), 1);
			
			//add new entities from last tick
			for each (entity in model.entitiesToAdd)
			model.entities.push(entity);
			
			//clear both lists.
			model.entitiesToRemove.splice(0, model.entitiesToRemove.length);
			model.entitiesToAdd.splice(0, model.entitiesToAdd.length);
		}
	}
}